package com.ruoyi.project.test.gds.formal.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.test.gds.formal.File2Hex;
import com.ruoyi.project.test.gds.formal.GdsProcess;
import com.ruoyi.project.test.gds.formal.mapper.GdsFileMapper;
import com.ruoyi.project.test.gds.formal.model.GdsFile;
import com.ruoyi.project.test.gds.formal.model.ProcessInfo;
import com.ruoyi.project.test.gds.formal.model.Software;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/gds")
@CrossOrigin
@Slf4j
public class GdsController extends BaseController
{

    @Autowired
    public File2Hex file2Hex;
    @Autowired
    public GdsProcess gdsProcess;
    @Autowired
    public GdsFileMapper gdsFileMapper;

    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file)
    {

        try {
            if (file != null){
                if (!file.isEmpty()){
                    file2Hex.convertToHex(file.getBytes());
                }else {
                    AjaxResult.error("文件为空");
                }
            }else {
                AjaxResult.error("文件参数为空");
            }
        }catch (Exception e){
            log.error("gds.upload异常",e);
            return AjaxResult.error(e.getMessage());
        }
        return success();
    }
    @PostMapping("/select")
    @ResponseBody
    public AjaxResult select(@RequestBody GdsFile gdsFile)
    {
        return AjaxResult.success(gdsFileMapper.selectGdsFileList(gdsFile));
    }
    @PostMapping("/selectById")
    @ResponseBody
    public AjaxResult selectById(@RequestParam String fileId)
    {
        return AjaxResult.success(gdsFileMapper.selectGdsFileById(fileId));
    }
    @PostMapping("/process")
    @ResponseBody
    public AjaxResult process(@RequestBody ProcessInfo processInfo)
    {
        gdsProcess.outputProcessData(processInfo);
        return success();
    }
    @PostMapping("/upgrade")
    @ResponseBody
    public AjaxResult upgrade(@RequestBody String deviceIds,String softwareId)
    {
//        System.out.println(file.getSize());
//        System.out.println(file.getOriginalFilename());
//        System.out.println(software.toString());
        System.out.println(deviceIds+softwareId);
        return success();
    }
}
